package de.szut.fragments;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TabHost;
import de.szut.main.ResourcesManager;
import de.szut.scene.helper.SceneManager;
import de.szut.scene.scenes.GameScene;
import de.szut.splasher.R;

/**
 * Created with IntelliJ IDEA.
 * User: nbothmer
 * Date: 10.10.13
 * Time: 16:22
 * To change this template use File | Settings | File Templates.
 */
public class WorldMenuGenerator {
    private ResourcesManager resourcesManager;
    private Context context;
    private FragmentActivity activity;
    private WorldMenuTabManager tabManager;
    private String worldTabTag = "world_fragment";
    private String spreadingTabTag = "spreading_fragment";

    public WorldMenuGenerator(Context ctx, FragmentActivity activity) {
        this.context = ctx;
        this.resourcesManager = ResourcesManager.getInstance();
        this.activity = activity;

    }

    public View generate() {
        View layout = this.getBaseLayout();
        final TabHost tabHost = (TabHost) layout.findViewById(android.R.id.tabhost);
        ImageButton cancelBtn = (ImageButton) layout.findViewById(R.id.world_menu_cancel);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resourcesManager.getActivity().getWorldMenu().setVisibility(View.GONE);
                ((GameScene) SceneManager.getInstance().getCurrentScene()).setSceneIsTouchable(true);
                if (((GameScene) SceneManager.getInstance().getCurrentScene()).isTimeIsRunning()) {
                    ((GameScene) SceneManager.getInstance().getCurrentScene()).reRunTimer();
                }

            }
        });
        tabHost.setup();
        tabManager = new WorldMenuTabManager(activity, tabHost, R.id.realtabcontent);
        tabManager.addTab(tabHost.newTabSpec(worldTabTag).setIndicator("Welt"), WorldTabFragment.class, null);
        tabManager.addTab(tabHost.newTabSpec(spreadingTabTag).setIndicator("Ausbreitung"), SpreadingTabFragment.class, null);
        return layout;
    }

    private View getBaseLayout() {
        LayoutInflater inflater = LayoutInflater.from(this.context);
        return inflater.inflate(R.layout.world_menu_view, null);
    }

//    public void reloadValues(){
//        WorldTabFragment worldTabFragment = (WorldTabFragment)activity.getSupportFragmentManager().findFragmentByTag(worldTabTag);
//        worldTabFragment.setValues();
//    }


    public WorldMenuTabManager getTabManager() {
        return tabManager;
    }
}
